package User;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LibraryApp extends JFrame {

    private String username;
    private Connection conn;
    private DefaultTableModel tableModel;
    private JTable table;
    private JPanel contentPanel; // 新增用于承载组件的面板

    public LibraryApp(String username) {
        this.username = username;
        setTitle("借阅记录");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookmanagement", "root", "root");
            tableModel = new DefaultTableModel(new String[]{"书名", "借阅时间", "操作"}, 0);
            displayBorrowRecords();
            table = new JTable(tableModel);
            table.getColumnModel().getColumn(2).setCellRenderer(new ButtonRenderer());
            table.addMouseListener((MouseListener) new TableMouseListener());
            JScrollPane scrollPane = new JScrollPane(table);
            contentPanel.add(scrollPane, BorderLayout.CENTER);
            setContentPane(contentPanel);
            setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void displayBorrowRecords() {
        try {
            String query = "SELECT borrowbook, borrowtime FROM borrowrecord WHERE borrowname =? AND borrowcondition IS NULL";
            PreparedStatement pstmt = conn.prepareStatement(query);
            pstmt.setString(1, username);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                String borrowBook = rs.getString("borrowbook");
                String borrowTime = rs.getString("borrowtime");

                tableModel.addRow(new Object[]{borrowBook, borrowTime, "还书"});
            }
            rs.close();
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    class ButtonRenderer extends JButton implements TableCellRenderer {
        public ButtonRenderer() {
            setOpaque(true);
        }
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean cellHasFocus, int row, int column) {
            setText((value == null)? "" : value.toString());
            return this;
        }
    }
    class TableMouseListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            int row = table.getSelectedRow();
            int column = table.getSelectedColumn();

            if (column == 2) {
                String borrowBook = (String) tableModel.getValueAt(row, 0);
                returnBook(borrowBook);
            }
        }
    }
    private void returnBook(String borrowBook) {
        try {
            String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            String insertQuery = "INSERT INTO returnrecord (returnname, returnbook, returntime) VALUES (?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(insertQuery);
            pstmt.setString(1, username);
            pstmt.setString(2, borrowBook);
            pstmt.setString(3, currentTime);
            pstmt.executeUpdate();
            pstmt.close();
            String updateQuery = "UPDATE borrowrecord SET borrowcondition = '已归还' WHERE borrowbook =? AND borrowname =?";
            PreparedStatement pstmtUpdate = conn.prepareStatement(updateQuery);
            pstmtUpdate.setString(1, borrowBook);
            pstmtUpdate.setString(2, username);
            pstmtUpdate.executeUpdate();
            pstmtUpdate.close();
            updateTable();
            JOptionPane.showMessageDialog(null, "还书成功！");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    public void updateTable() {
        tableModel.setRowCount(0);
        displayBorrowRecords();
    }
    public JPanel getContentPanel() {
        return contentPanel;
    }

 /*public void refreshBorrowedBooks() {
        tableModel.setRowCount(0);
        displayBorrowRecords();
        table.setModel(tableModel);
        table.revalidate();
        table.repaint();
    }*/
}